<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片试图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            @clear="getGoodsList"
            @keyup.enter.native="getGoodsList"
            v-model.trim="queryInfo.query"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goGoodsAdd">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表区域 -->
      <el-table :data="goodsList" border stripe style="width: 100%">
        <el-table-column type="index" label="序号">
          <template v-slot="scope">
            <span>{{(queryInfo.pagenum - 1) * queryInfo.pagesize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="goods_price" width="240px" label="商品价格(元)"></el-table-column>
        <el-table-column prop="goods_number" width="140px" label="商品数量"></el-table-column>
        <el-table-column prop="goods_state" width="140px" label="商品状态">
          <template v-slot="scope">
            <el-button
              size="mini"
              v-if="!scope.row.goods_state || scope.row.goods_state === 0"
              type="warning"
            >未通过</el-button>
            <el-button size="mini" v-else-if="scope.row.goods_state === 1" type="info">审核中</el-button>
            <el-button size="mini" v-else type="success">已审核</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="add_time" width="240px" label="创建时间">
          <template
            v-slot="scope"
          >{{new Date(scope.row.add_time) | dateFormat('yyyy-mm-dd HH:MM:ss')}}</template>
        </el-table-column>
        <el-table-column label="操作" width="280px">
          <template v-slot="scope">
            <el-tooltip class="item" effect="dark" :enterable="false" content="修改" placement="top">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="goGoodsEdit(scope.row.goods_id)"
              ></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" :enterable="false" content="删除" placement="top">
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="removeById(scope.row.goods_id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页显示 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      total: 0,
      // 获取查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      addDialogVisible: true
    }
  },
  methods: {
    async getGoodsList() {
      if (this.queryInfo.query) {
        this.queryInfo.pagenum = 1
      }
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getGoodsList()
    },
    goGoodsAdd() {
      this.$router.push('/goods/add')
    },
    goGoodsEdit(id) {
      this.$router.push('/goods/edit/' + id)
    },
    async removeById(id) {
      this.$confirm('此操作将永久删除该参数, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`goods/${id}`)
          if (res.meta.status !== 200) {
            this.$message.error(res.meta.msg)
          }
          this.$message.success('删除成功')
          this.getGoodsList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  },
  created() {
    this.getGoodsList()
  }
}
</script>
<style lang="less" scoped>
</style>
